<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>LibreTV - 免费在线视频搜索与观看平台</title>
    <meta name="description" content="LibreTV是一个免费的在线视频搜索平台，无广告、安全，提供来自多个视频源的内容搜索与观看服务，无需注册即可使用。">
    <meta name="keywords" content="在线视频,免费视频,视频搜索,电影,电视剧,LibreTV">
    <meta name="author" content="LibreTV Team">

    <!-- Favicon -->
    <link rel="icon" href="./image/retrotv_5520.png">
    <link rel="apple-touch-icon" href="./image/retrotv_5520.png">
    <link rel="manifest" href="manifest.json">
    
    <script src="libs/tailwindcss.min.js"></script>
    <link rel="stylesheet" href="css/styles.css">
</head>
<body class="page-bg text-white flex flex-col min-h-screen">
    <!-- Main Content Area -->
    <main class="flex-grow pb-16"> <!-- pb-16 to make space for fixed bottom nav -->
        <!-- Tab Content: Home -->
        <div id="page-home" class="page-content hidden container mx-auto px-4">
            <!-- 网站标志和口号 -->
            <header class="text-center mb-6 pt-12"> <!-- Increased bottom margin & Added top padding -->
                <div class="flex justify-center items-center mb-2"> <!-- Reduced bottom margin -->
                    <a href="#" onclick="navigateToTab('home'); return false;" class="flex items-center">
                        <svg class="w-8 h-8 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <!-- Slightly smaller logo -->
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
                        </svg>
                        <h1 class="text-4xl font-bold gradient-text">LibreTV</h1> <!-- Slightly smaller title -->
                    </a>
                </div>
                <!-- Slogan removed for a cleaner mobile home screen, or can be placed elsewhere if desired -->
                <!-- <p class="text-gray-400 mb-8">自由观影，畅享精彩</p> -->
            </header>

            <!-- New Home Screen Content Area for Carousels -->
            <div id="home-content-area" class="space-y-8">
                <!-- Example Carousel Row (to be dynamically populated) -->
                <!--
                <section class="carousel-section">
                    <div class="flex justify-between items-center mb-3 px-1">
                        <h2 class="text-xl font-semibold text-white">热门电影</h2>
                        <button class="text-sm text-pink-400 hover:text-pink-300">查看全部 &rarr;</button>
                    </div>
                    <div class="carousel-container overflow-x-auto pb-2">
                        <div class="carousel-track flex space-x-3">
                            <!- Carousel items (cards) will be injected here by douban.js ->
                        </div>
                    </div>
                </section>
                -->
                <!-- More carousel sections will be added here by douban.js -->
                 <!-- Tags - Placed once at the top of home content -->
                <div class="mb-6">
                    <!-- Movie/TV Toggle Removed -->
                    <div class="overflow-x-auto pb-2">
                        <div id="douban-tags" class="flex space-x-2 min-w-max justify-center">
                            <!-- Douban tags will be rendered here by douban_ui.js -->
                        </div>
                    </div>
                </div>

                <!-- Container for Douban results (which will be styled as carousels) -->
                <!-- This div will be populated by douban.js with sections and carousels -->
                <div id="douban-recommendations-container">
                    <!-- This is where douban.js will inject rows like "豆瓣热门", "最新电影" etc. -->
                    <!-- Each row will contain a title and a div for cards (e.g., #douban-results-hot, #douban-results-latest) -->
                </div>

                <!-- Removed the new filter section from homepage -->

            </div>
        </div>

        <!-- Tab Content: Search -->
        <div id="page-search" class="page-content hidden container mx-auto px-4 py-8">
            <div id="searchArea" class="flex flex-col items-center">
                <div class="w-full max-w-2xl">
                    <div class="flex items-stretch mb-3 h-14 shadow-lg rounded-lg overflow-hidden">
                        <!-- 首页按钮 (Removed as part of search bar, search is now a tab) -->
                        <!-- 搜索输入 -->
                        <input type="text" 
                               id="searchInput" 
                               class="flex-1 bg-[#111] border-y border-l border-[#333] text-white px-6 py-0 focus:outline-none transition-colors rounded-l-lg" 
                               placeholder="搜索你喜欢的视频..." 
                               autocomplete="off"
                               aria-label="视频搜索框">
                        <!-- 搜索按钮 -->
                        <button onclick="search()" 
                                class="w-20 sm:w-24 flex items-center justify-center bg-white text-black font-medium hover:bg-gray-200 transition-colors rounded-r-lg" 
                                aria-label="搜索按钮">
                            搜索
                        </button>
                    </div>
                    
                    <!-- 添加最近搜索记录部分 -->
                    <div id="recentSearches" class="mt-4 flex flex-wrap gap-2" aria-label="最近搜索记录">
                        <!-- 这里会动态插入最近的搜索记录 -->
                    </div>
                </div>
            </div>

             <!-- 搜索结果：初始隐藏 -->
            <div id="resultsArea" class="w-full hidden mt-8">
                <div class="mx-auto max-w-7xl px-2"> <!-- 添加最大宽度限制并居中 -->
                    <div class="flex justify-end items-center mb-4">
                        <div class="text-sm text-gray-400">
                            <span id="searchResultsCount">0</span> 个结果
                        </div>
                    </div>
                    <!-- 修改网格布局以适应大一些的横向卡片 -->
                    <div id="results" class="grid gap-4">
                        <!-- 结果将在这里动态生成 -->
                    </div>
                </div>
            </div>
        </div>

        <!-- Tab Content: Filter -->
        <div id="page-filter" class="page-content hidden container mx-auto px-4 py-8">
            <h3 class="text-xl font-semibold text-white mb-4 text-center gradient-text">豆瓣影视筛选</h3>
            <!-- Container for Douban filter controls -->
            <div id="douban-filter-controls-area" class="w-full max-w-4xl mx-auto mb-6">
                <!-- Douban filter buttons will be rendered here by initDoubanFilterControls -->
            </div>
            <!-- Container for Douban filter results -->
            <div id="douban-filter-results-area" class="w-full">
                <div class="mx-auto max-w-7xl px-2">
                    <div class="flex justify-end items-center mb-4">
                        <div class="text-sm text-gray-400">
                            <span id="doubanFilterResultsCount">0</span> 个结果
                        </div>
                    </div>
                    <div id="douban-filter-items-grid" class="grid gap-4">
                        <!-- Douban filter results (cards) will be rendered here -->
                    </div>
                </div>
            </div>
        </div>

        <!-- Tab Content: History -->
        <div id="page-history" class="page-content hidden container mx-auto px-4 py-8">
            <div class="flex justify-between items-center mb-6">
                <h3 class="text-2xl font-bold gradient-text">观看历史</h3>
                <button onclick="clearViewingHistory()" class="px-4 py-2 bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 hover:from-indigo-600 hover:via-purple-600 hover:to-pink-600 text-white rounded-lg text-sm transition-all duration-300 shadow-md hover:shadow-lg">
                    清空历史记录
                </button>
            </div>
            <div id="historyList" class="pb-4">
                <!-- 历史记录将在这里动态显示 -->
                <div class="text-center text-gray-500 py-8">暂无观看记录</div>
            </div>
        </div>

        <!-- Tab Content: Settings -->
        <div id="page-settings" class="page-content hidden container mx-auto px-4 py-8">
            <div class="flex justify-between items-center mb-6">
                <h3 class="text-2xl font-bold gradient-text">设置</h3>
            </div>
            <div class="space-y-5 max-w-2xl mx-auto">
                 <!-- 数据源设置区域 -->
                <div class="p-3 bg-[#151515] rounded-lg shadow-inner">
                    <label class="block text-sm font-medium text-gray-400 mb-3 border-b border-[#333] pb-1">数据源设置</label>
                    
                    <!-- 批量操作按钮 -->
                    <div class="flex space-x-2 mb-3">
                        <button onclick="selectAllAPIs(true)" class="px-2 py-1 bg-[#333] hover:bg-[#444] text-white text-xs rounded">全选</button>
                        <button onclick="selectAllAPIs(false)" class="px-2 py-1 bg-[#333] hover:bg-[#444] text-white text-xs rounded">全不选</button>
                        <button onclick="selectAllAPIs(true, true)" class="px-2 py-1 bg-[#333] hover:bg-[#444] text-white text-xs rounded">全选普通资源</button>
                    </div>
                    
                    <!-- API选择区域 - 使用滚动区域 -->
                    <div class="max-h-40 overflow-y-auto bg-[#191919] p-2 rounded-lg mb-3">
                        <div id="apiCheckboxes">
                            <!-- 这里将动态插入API复选框 -->
                        </div>
                    </div>
                    
                    <!-- API信息显示 -->
                    <div class="text-xs text-gray-500 flex justify-between items-center">
                        <span>已选API数量：<span id="selectedApiCount" class="text-white">0</span></span>
                        <span id="siteStatus" class="ml-2"></span>
                    </div>
                </div>

                <!-- 自定义API管理区域 -->
                <div class="p-3 bg-[#151515] rounded-lg shadow-inner">
                    <div class="flex justify-between items-center mb-2">
                        <label class="block text-sm font-medium text-gray-400 border-b border-[#333] w-full pb-1">自定义API</label>
                        <button onclick="showAddCustomApiForm()" class="bg-[#333] hover:bg-[#444] text-white w-6 h-6 rounded-full text-center leading-none text-lg ml-1">+</button>
                    </div>
                    <div id="customApisList" class="max-h-32 overflow-y-auto mb-2">
                        <!-- 自定义API将显示在这里 -->
                    </div>
                    
                    <!-- 添加自定义API表单 (默认隐藏) -->
                    <div id="addCustomApiForm" class="hidden mt-2 p-2 bg-[#191919] rounded-lg">
                        <input type="text" id="customApiName" placeholder="API名称" class="w-full bg-[#222] border border-[#333] text-white px-2 py-1 rounded mb-2" autocomplete="off">
                        <input type="text" id="customApiUrl" placeholder="https://abc.com" class="w-full bg-[#222] border border-[#333] text-white px-2 py-1 rounded mb-2" autocomplete="off">
                        <!-- 新增 detail 地址输入框 -->
                        <input type="text" id="customApiDetail" placeholder="detail地址（可选）" class="w-full bg-[#222] border border-[#333] text-white px-2 py-1 rounded mb-2" autocomplete="off">
                        <!-- 添加成人内容切换 -->
                        <div class="flex items-center mb-2">
                            <input type="checkbox" id="customApiIsAdult" class="form-checkbox h-4 w-4 text-pink-500 bg-[#222] border border-[#333]">
                            <label for="customApiIsAdult" class="ml-2 text-xs text-pink-400">黄色资源站</label>
                        </div>
                        <div class="flex space-x-2">
                            <button onclick="addCustomApi()" class="bg-blue-600 hover:bg-blue-700 text-white px-3 py-1 rounded text-xs">添加</button>
                            <button onclick="cancelAddCustomApi()" class="bg-[#444] hover:bg-[#555] text-white px-3 py-1 rounded text-xs">取消</button>
                        </div>
                    </div>
                </div>
                
                <!-- 内容过滤设置区域 -->
                <div class="p-3 bg-[#151515] rounded-lg shadow-inner">
                    <label class="block text-sm font-medium text-gray-400 mb-3 border-b border-[#333] pb-1">功能开关</label>
                    
                    <!-- 黄色内容过滤开关 -->
                    <div class="flex flex-col mb-3 pb-3 border-b border-[#222] relative">
                        <div class="flex items-center justify-between">
                            <div>
                                <label class="text-sm font-medium text-gray-400">黄色内容过滤</label>
                                <p class="text-xs text-gray-500 mt-1 filter-description">过滤"伦理片"等黄色内容</p>
                            </div>
                            <div class="relative inline-block w-12 align-middle select-none">
                                <input type="checkbox" id="yellowFilterToggle" class="opacity-0 absolute w-full h-full cursor-pointer z-10">
                                <div class="toggle-bg bg-[#333] w-12 h-6 rounded-full transition-colors duration-300 ease-in-out"></div>
                                <div class="toggle-dot absolute w-5 h-5 bg-white rounded-full top-0.5 left-0.5 transition-transform duration-300 ease-in-out"></div>
                            </div>
                        </div>
                        <!-- 警告提示将在这里动态插入 -->
                    </div>
                    
                    <!-- 广告过滤开关 -->
                    <div class="flex items-center justify-between mb-3 pb-3 border-b border-[#222]">
                        <div>
                            <label class="text-sm font-medium text-gray-400">分片广告过滤</label>
                            <p class="text-xs text-gray-500 mt-1">关闭可减少旧版浏览器卡顿</p>
                        </div>
                        <div class="relative inline-block w-12 align-middle select-none">
                            <input type="checkbox" id="adFilterToggle" class="opacity-0 absolute w-full h-full cursor-pointer z-10">
                            <div class="toggle-bg bg-[#333] w-12 h-6 rounded-full transition-colors duration-300 ease-in-out"></div>
                            <div class="toggle-dot absolute w-5 h-5 bg-white rounded-full top-0.5 left-0.5 transition-transform duration-300 ease-in-out"></div>
                        </div>
                    </div>
                    
                    <!-- 豆瓣热门开关 -->
                    <div class="flex items-center justify-between">
                        <div>
                            <label class="text-sm font-medium text-gray-400">豆瓣热门推荐</label>
                            <p class="text-xs text-gray-500 mt-1">首页显示豆瓣热门影视内容</p>
                        </div>
                        <div class="relative inline-block w-12 align-middle select-none">
                            <input type="checkbox" id="doubanToggle" class="opacity-0 absolute w-full h-full cursor-pointer z-10">
                                <div class="toggle-bg bg-[#333] w-12 h-6 rounded-full transition-colors duration-300 ease-in-out"></div>
                                <div class="toggle-dot absolute w-5 h-5 bg-white rounded-full top-0.5 left-0.5 transition-transform duration-300 ease-in-out"></div>
                            </div>
                        </div>
                    </div>

                    <!-- Douban API Mode Switch -->
                    <div class="flex items-center justify-between pt-3 border-t border-[#222] mt-3">
                        <div>
                            <label for="doubanApiModeToggle" class="text-sm font-medium text-gray-400">豆瓣API模式</label>
                            <p class="text-xs text-gray-500 mt-1">选择使用全部豆瓣接口或仅旧接口</p>
                        </div>
                        <div class="relative inline-block w-12 align-middle select-none">
                            <input type="checkbox" id="doubanApiModeToggle" name="doubanApiModeToggle" class="opacity-0 absolute w-full h-full cursor-pointer z-10">
                            <div class="toggle-bg bg-[#333] w-12 h-6 rounded-full transition-colors duration-300 ease-in-out"></div>
                            <div class="toggle-dot absolute w-5 h-5 bg-white rounded-full top-0.5 left-0.5 transition-transform duration-300 ease-in-out"></div>
                        </div>
                    </div>
                </div>

                <!-- 一般功能区域 -->
                <div class="p-3 bg-[#151515] rounded-lg shadow-inner">
                    <label class="block text-sm font-medium text-gray-400 mb-3 border-b border-[#333] pb-1">一般功能</label>
                    <button onclick="importConfig()" class="px-4 py-2 w-full bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 hover:from-indigo-600 hover:via-purple-600 hover:to-pink-600 text-white rounded-lg text-sm transition-all duration-300 shadow-md hover:shadow-lg mb-2">导入配置</button>
                    <button onclick="exportConfig()" class="px-4 py-2 mb-2 w-full bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 hover:from-indigo-600 hover:via-purple-600 hover:to-pink-600 text-white rounded-lg text-sm transition-all duration-300 shadow-md hover:shadow-lg">导出配置</button>
                    <button onclick="clearLocalStorage()" class="px-4 py-2 w-full bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 hover:from-indigo-600 hover:via-purple-600 hover:to-pink-600 text-white rounded-lg text-sm transition-all duration-300 shadow-md hover:shadow-lg">清除Cookie</button>
                </div>
            </div>
        </div>

        <!-- Tab Content: Category View (for infinite scroll) -->
        <div id="page-category-view" class="page-content hidden container mx-auto px-4 py-8">
            <div class="flex items-center mb-6">
                <button onclick="navigateToTab(sessionStorage.getItem('previousTab') || 'home')" class="text-pink-400 hover:text-pink-300 mr-4">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
                    </svg>
                </button>
                <h2 id="category-view-title" class="text-2xl font-bold gradient-text">Category Name</h2>
            </div>
            <div id="category-items-grid" class="grid gap-4">
                <!-- Category items will be loaded here -->
            </div>
            <div id="category-loading-spinner" class="text-center py-8 hidden">
                <div class="w-8 h-8 border-4 border-pink-500 border-t-transparent rounded-full animate-spin mx-auto"></div>
                <p class="text-gray-400 mt-2">加载更多...</p>
            </div>
        </div>
    </main>

    <!-- Bottom Navigation Bar -->
    <nav id="bottomNav" class="fixed bottom-0 left-0 right-0 bg-[#111] border-t border-[#333] flex justify-around items-center h-16 z-50">
        <button data-page="home" class="nav-item flex flex-col items-center justify-center text-gray-400 hover:text-white transition-colors p-2 active">
            <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"></path></svg>
            <span class="text-xs">首页</span>
        </button>
        <button data-page="search" class="nav-item flex flex-col items-center justify-center text-gray-400 hover:text-white transition-colors p-2">
            <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>
            <span class="text-xs">搜索</span>
        </button>
        <button data-page="filter" class="nav-item flex flex-col items-center justify-center text-gray-400 hover:text-white transition-colors p-2">
            <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4a1 1 0 011-1h16a1 1 0 011 1v2.586a1 1 0 01-.293.707l-6.414 6.414a1 1 0 00-.293.707V17l-4 4v-6.586a1 1 0 00-.293.707L3.293 7.293A1 1 0 013 6.586V4z"></path></svg>
            <span class="text-xs">筛选</span>
        </button>
        <button data-page="history" class="nav-item flex flex-col items-center justify-center text-gray-400 hover:text-white transition-colors p-2">
            <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
            <span class="text-xs">历史</span>
        </button>
        <button data-page="settings" class="nav-item flex flex-col items-center justify-center text-gray-400 hover:text-white transition-colors p-2">
            <svg class="w-6 h-6 mb-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
            <span class="text-xs">设置</span>
        </button>
    </nav>

    <!-- 页脚区域 (Removed, as bottom nav takes its place for primary navigation) -->
    <!-- <footer class="footer mt-8 py-6 border-t border-[#333] bg-[#0a0a0a]">
        <div class="container mx-auto px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <div class="flex items-center justify-center md:justify-start">
                        <svg class="w-6 h-6 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 10l4.553-2.276A1 1 0 0121 8.618v6.764a1 1 0 01-1.447.894L15 14M5 18h8a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
                        </svg>
                        <span class="gradient-text font-bold">LibreTV</span>
                    </div>
                    <p class="text-gray-500 text-sm mt-2 text-center md:text-left">© 2025 LibreTV - 自由观影，畅享精彩</p>
                </div>
                
                <div class="text-center md:text-right">
                    <p class="text-gray-500 text-sm max-w-md">
                        免责声明：本站仅为视频搜索工具，不存储、上传或分发任何视频内容。
                        所有视频均来自第三方API接口。如有侵权，请联系相关内容提供方。
                    </p>
                    <div class="mt-2 flex justify-center md:justify-end space-x-4">
                        <a href="about.html" class="text-gray-400 hover:text-white text-sm transition-colors">关于我们</a>
                        <a href="privacy.html" class="text-gray-400 hover:text-white text-sm transition-colors">隐私政策</a>
                        <a href="https://www.unicef.org/zh" target="_blank" rel="noopener" class="text-blue-400 hover:text-blue-300 text-sm transition-colors">捐赠</a>
                    </div>
                </div>
            </div>
        </div>
    </footer>

    <!-- 详情模态框 -->
    <div id="modal" class="fixed inset-0 bg-black/95 hidden flex items-center justify-center transition-opacity duration-300 z-40">
        <div class="bg-[#111] p-8 rounded-lg w-11/12 max-w-4xl border border-[#333] max-h-[90vh] flex flex-col">
            <div class="flex justify-between items-center mb-6 flex-none">
                <h2 id="modalTitle" class="text-2xl font-bold gradient-text break-words pr-4 max-w-[80%]"></h2>
                <button onclick="closeModal()" class="text-gray-400 hover:text-white text-2xl transition-colors flex-shrink-0">&times;</button>
            </div>
            <div id="modalContent" class="overflow-auto flex-1 min-h-0">
                <div class="grid grid-cols-2 sm:grid-cols-4 md:grid-cols-6 lg:grid-cols-8 gap-2">
                </div>
            </div>
        </div>
    </div>

    <!-- 自定义标签输入模态框 -->
    <div id="customTagModal" class="fixed inset-0 bg-black/90 hidden items-center justify-center z-50 transition-opacity duration-300">
        <div class="bg-[#1a1a1a] p-6 rounded-lg shadow-xl w-11/12 max-w-sm border border-[#333]">
            <h3 class="text-lg font-semibold text-white mb-4">添加自定义标签</h3>
            <input type="text" id="customTagInput" class="w-full bg-[#222] border border-[#444] text-white px-3 py-2 rounded mb-4 focus:outline-none focus:border-pink-500" placeholder="请输入标签名称...">
            <div class="flex justify-end space-x-3">
                <button id="cancelCustomTagBtn" class="px-4 py-2 bg-[#333] hover:bg-[#444] text-gray-300 rounded text-sm transition-colors">取消</button>
                <button id="confirmCustomTagBtn" class="px-4 py-2 bg-pink-600 hover:bg-pink-700 text-white rounded text-sm transition-colors">确认</button>
            </div>
        </div>
    </div>

    <!-- 密码验证弹窗 -->
    <div id="passwordModal" class="fixed inset-0 bg-black/95 hidden items-center justify-center z-[65] transition-opacity duration-300">
        <div class="bg-[#111] p-8 rounded-lg w-11/12 max-w-md border border-[#333] max-h-[90vh] flex flex-col">
            <div class="flex justify-between items-center mb-6 flex-none">
                <h2 class="text-2xl font-bold gradient-text">访问验证</h2>
            </div>
            <div class="mb-6">
                <p class="text-gray-300 mb-4">请输入密码继续访问</p>
                <form id="passwordForm" onsubmit="handlePasswordSubmit(); return false;">
                    <input type="text" name="username" id="username" autocomplete="username" style="display:none" tabindex="-1" aria-hidden="true">
                    <input type="password" id="passwordInput" class="w-full bg-[#111] border border-[#333] text-white px-4 py-3 rounded-lg focus:outline-none focus:border-white transition-colors" placeholder="密码..." autocomplete="new-password">
                    <button id="passwordSubmitBtn" type="submit" class="mt-4 w-full bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">提交</button>
                </form>
                <p id="passwordError" class="text-red-500 mt-2 hidden">密码错误，请重试</p>
            </div>
        </div>
    </div>

    <!-- 版权声明弹窗 -->
    <div id="disclaimerModal" class="fixed inset-0 bg-black/90 hidden items-center justify-center z-[60]">
        <div class="bg-[#111] p-8 rounded-lg border border-[#333] w-11/12 max-w-2xl max-h-[90vh] overflow-y-auto">
            <h2 class="text-2xl font-bold gradient-text mb-6 text-center">使用声明</h2>
            <div class="text-gray-300 space-y-4">
                <p>
                    欢迎使用 LibreTV。在开始使用前，请您了解并同意以下条款：
                </p>
                <p>
                    <strong class="text-blue-400">服务性质：</strong> LibreTV 仅提供视频搜索服务，不直接提供、存储或上传任何视频内容。所有搜索结果均来自第三方公开接口。
                </p>
                <p>
                    <strong class="text-blue-400">用户责任：</strong> 用户在使用本站服务时，须遵守相关法律法规，不得利用搜索结果从事侵权行为，如下载、传播未经授权的作品等。
                </p>
                <p>
                    <strong class="text-blue-400">广告风险提示：</strong> 本站所有视频均来自第三方采集站，视频中出现的广告与本站无关，请勿相信或点击视频中的任何广告内容，谨防上当受骗。
                </p>
            </div>
            <div class="mt-6 flex justify-center">
                <button id="acceptDisclaimerBtn" class="px-6 py-3 bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 text-white font-semibold rounded-lg hover:shadow-lg transition-all duration-300">
                    我已了解并接受
                </button>
            </div>
        </div>
    </div>

    <!-- 错误提示框 -->
    <div id="toast" class="fixed top-4 left-1/2 -translate-x-1/2 bg-red-500 text-white px-6 py-3 rounded-lg shadow-lg transform transition-all duration-300 opacity-0 -translate-y-full z-50">
        <p id="toastMessage"></p>
    </div>

    <!-- 添加 loading 提示框 -->
    <div id="loading" class="fixed inset-0 bg-black/80 hidden items-center justify-center z-50">
        <div class="bg-[#111] p-8 rounded-lg border border-[#333] flex items-center space-x-4">
            <div class="w-8 h-8 border-4 border-white border-t-transparent rounded-full animate-spin"></div>
            <p class="text-white text-lg">加载中...</p>
        </div>
    </div>

    <!-- JSON-LD 结构化数据 -->
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "WebSite",
        "name": "LibreTV",
        "url": "https://libretv.is-an.org/",
        "description": "免费在线视频搜索与观看平台",
        "potentialAction": {
            "@type": "SearchAction",
            "target": "https://libretv.is-an.org/?s={search_term_string}",
            "query-input": "required name=search_term_string"
        }
    }
    </script>

    <!-- 引入纯 JS sha256（HTTP 下依然可用） -->
    <script src="libs/sha256.min.js"></script>
    <script>
        // 保存原始 js‑sha256 实现，避免被 password.js 覆盖
        window._jsSha256 = window.sha256;
    </script>
    <script src="js/config.js"></script>
    <script src="js/ui.js"></script>
    <script src="js/apiUtils.js"></script>
    <script src="js/apiRequestCore.js"></script>
    <script src="js/apiSearchHandlers.js"></script>
    <script src="js/apiDetailHandlers.js"></script>
    <script src="js/api.js"></script>
    <script src="js/password.js"></script>
    <script src="js/app_globals.js"></script>
    <script src="js/app_init.js"></script>
    <script src="js/api_management.js"></script>
    <script src="js/app_event_listeners.js"></script>
    <script src="js/search_logic.js"></script>
    <script src="js/details_modal.js"></script>
    <script src="js/player_navigation.js"></script>
    <script src="js/config_io.js"></script>
    <script src="js/douban_api.js"></script>
    <script src="js/douban_ui.js"></script>
    <script src="js/douban_logic.js"></script>
    <script src="js/douban_filters.js"></script>
    <script src="js/douban.js"></script>

    <!-- PWA 注册 (暂时注释掉以避免 Tauri 环境下的协议错误) -->
    <!--
    <script>
        if ('serviceWorker' in navigator) {
            window.addEventListener('load', () => {
                navigator.serviceWorker.register('/service-worker.js');
            });
        }
    </script>
    -->

    <!-- 环境变量注入脚本 -->
    <script>
        // 创建全局环境变量对象
        window.__ENV__ = window.__ENV__ || {};
        
        // 注入服务器端环境变量 (将由服务器端替换)
        // PASSWORD 变量将在这里被服务器端注入
        window.__ENV__.PASSWORD = "{{PASSWORD}}";
    </script>

    <!-- 版本检测脚本 -->
    <script src="js/version-check.js"></script>

    <!-- 弹窗显示脚本 -->
    <script>
        // 在页面加载完成后检查用户是否首次访问
        document.addEventListener('DOMContentLoaded', function() {
            // 检查用户是否已经看过声明
            const hasSeenDisclaimer = localStorage.getItem('hasSeenDisclaimer');
            
            if (!hasSeenDisclaimer) {
                // 显示弹窗
                const disclaimerModal = document.getElementById('disclaimerModal');
                disclaimerModal.style.display = 'flex';
                
                // 添加接受按钮事件
                document.getElementById('acceptDisclaimerBtn').addEventListener('click', function() {
                    // 保存用户已看过声明的状态
                    localStorage.setItem('hasSeenDisclaimer', 'true');
                    // 隐藏弹窗
                    disclaimerModal.style.display = 'none';

                    console.log("Disclaimer accepted. Attempting to enable and refresh Douban content via toggle event.");

                    // 1. 确保 localStorage 中 doubanEnabled 为 true
                    localStorage.setItem('doubanEnabled', 'true');
                    console.log("Ensured doubanEnabled is 'true' in localStorage.");

                    // 2. 获取豆瓣设置的开关元素
                    const doubanToggleSwitch = document.getElementById('doubanToggle');
                    if (doubanToggleSwitch) {
                        // 3. 如果开关当前不是选中状态，则更新它并触发 change 事件
                        //    这将利用现有的事件监听器来更新 localStorage 并刷新内容。
                        if (!doubanToggleSwitch.checked) {
                            doubanToggleSwitch.checked = true;
                            // 手动更新UI视觉效果 (因为直接设置 .checked 不会触发所有CSS伪类相关的更新)
                            const toggleBg = doubanToggleSwitch.nextElementSibling;
                            const toggleDot = toggleBg ? toggleBg.nextElementSibling : null;
                            if (toggleBg && toggleDot) {
                                toggleBg.classList.add('bg-pink-600');
                                toggleDot.classList.add('translate-x-6');
                            }
                            console.log("Douban toggle was off, set to on and dispatching change event.");
                            doubanToggleSwitch.dispatchEvent(new Event('change', { bubbles: true }));
                        } else {
                            // 如果开关已经是选中状态，localStorage 也应该是 true。
                            // 此时，可能需要直接刷新内容，因为 change 事件不会因状态未变而触发。
                            console.log("Douban toggle already on. Ensuring home page is active and calling initDouban.");
                            sessionStorage.removeItem('homePageState'); // Clear old state
                            const pageHome = document.getElementById('page-home');
                            if (pageHome) pageHome.classList.remove('hidden');
                            document.querySelectorAll('.page-content').forEach(page => {
                                if (page.id !== 'page-home') page.classList.add('hidden');
                            });
                            const navItems = document.querySelectorAll('#bottomNav .nav-item');
                            navItems.forEach(item => {
                                item.classList.remove('active');
                                if (item.dataset.page === 'home') item.classList.add('active');
                            });
                            sessionStorage.setItem('activeTab', 'home');
                            if (typeof initDouban === 'function') {
                                setTimeout(function() {
                                    initDouban({ forceMainView: true });
                                }, 50); // Short delay
                            }
                        }
                    } else {
                        console.warn("Douban toggle switch UI element not found. Falling back to direct initDouban.");
                        // Fallback if toggle not found: ensure home is visible and call initDouban
                        sessionStorage.removeItem('homePageState');
                        const pageHome = document.getElementById('page-home');
                        if (pageHome) pageHome.classList.remove('hidden');
                         document.querySelectorAll('.page-content').forEach(page => {
                            if (page.id !== 'page-home') page.classList.add('hidden');
                        });
                        const navItems = document.querySelectorAll('#bottomNav .nav-item');
                        navItems.forEach(item => {
                            item.classList.remove('active');
                            if (item.dataset.page === 'home') item.classList.add('active');
                        });
                        sessionStorage.setItem('activeTab', 'home');
                        if (typeof initDouban === 'function') {
                           setTimeout(function() {
                                initDouban({ forceMainView: true });
                            }, 150);
                        }
                    }
                });
            }
        });
    </script>
</body>
</html>
